from typing import Annotated

from fastapi import APIRouter, Query
from pydantic import StringConstraints

from app.puzzle.service.card_guess import (
    card_guess_check_easy,
    card_guess_check_hard,
    card_guess_generate_easy,
    card_guess_generate_hard,
    card_guess_solve_easy,
    card_guess_solve_hard,
)

card_guess_router = APIRouter(
    prefix="/card_guess",
    tags=["Card Guess Puzzle"],
    responses={404: {"description": "Not found"}},
)


@card_guess_router.get(
    "/generate_easy",
    summary="扑克生成(简易规则版)",
    description="""
    生成5张扑克,但是根据特定规则隐藏一张,然后按照规则排序剩余4张,并展示给你
    如果你推理出了隐藏扑克,调用/card_guess/check检查是否正确
    """,
)
def card_guess_generate_easy_route():
    return card_guess_generate_easy()


@card_guess_router.get(
    "/solve_easy",
    summary="扑克猜谜(简易规则版)",
    description="""
    给定5张扑克,按照一个简单规则隐藏一张扑克,并排序剩余4张扑克
    展示4张扑克,机器猜出隐藏的扑克
    """,
)
def card_guess_solve_easy_route(
    cards: Annotated[
        str,
        StringConstraints(
            pattern=r"^[♠♥♣♦](10|[2-9JQKA])(,[♠♥♣♦](10|[2-9JQKA])){3}$",
            strip_whitespace=True,
        ),
        Query(
            description="4张牌,格式为花色+大小, 比如：cards=♥Q,♣A,♦J,♠6",
            example="♥Q,♣A,♦J,♠6",
        ),
    ],
):
    return card_guess_solve_easy(cards)


@card_guess_router.get(
    "/check_easy",
    summary="扑克猜谜检查(简易规则版)",
    description="""
    给定一个排序的卡,以及一个答案,判断答案是否准确,如不准确,给出正确答案
    """,
)
def card_guess_check_easy_route(
    cards: Annotated[
        str,
        StringConstraints(
            pattern=r"^[♠♥♣♦](10|[2-9JQKA])(,[♠♥♣♦](10|[2-9JQKA])){3}$",
            strip_whitespace=True,
        ),
        Query(
            description="4张牌,格式为花色+大小, 比如：cards=♥Q,♣A,♦J,♠6",
            example="♥Q,♣A,♦J,♠6",
        ),
    ],
    answer: Annotated[
        str,
        StringConstraints(
            pattern=r"^[♠♥♣♦](10|[2-9JQKA])$",
            strip_whitespace=True,
        ),
        Query(
            description="隐藏的牌,格式为花色+大小, 比如：answer=♥Q",
            example="♥Q",
        ),
    ],
):
    return card_guess_check_easy(cards, answer)


@card_guess_router.get(
    "/generate_hard",
    summary="扑克生成(难度飙升版)",
    description="""
    生成5张扑克,但是根据特定规则隐藏一张,然后按照规则排序剩余4张,并展示给你
    如果你推理出了隐藏扑克,调用/card_guess/check检查是否正确
    """,
)
def card_guess_generate_hard_route():
    return card_guess_generate_hard()


@card_guess_router.get(
    "/solve_hard",
    summary="扑克猜谜(难度飙升版)",
    description="""
    给定5张扑克,按照一个简单规则隐藏一张扑克,并排序剩余4张扑克
    展示4张扑克,机器将猜出隐藏的扑克
    """,
)
def card_guess_solve_hard_route(
    cards: Annotated[
        str,
        StringConstraints(
            pattern=r"^[♠♥♣♦](10|[2-9JQKA])(,[♠♥♣♦](10|[2-9JQKA])){3}$",
            strip_whitespace=True,
        ),
        Query(
            description="4张牌,格式为花色+大小, 比如：cards=♥Q,♣A,♦J,♠6",
            example="♥Q,♣A,♦J,♠6",
        ),
    ],
):
    return card_guess_solve_hard(cards)


@card_guess_router.get(
    "/check_hard",
    summary="扑克猜谜检查(难度飙升版)",
    description="""
    给定一个排序的卡,以及一个答案,判断答案是否准确,如不准确,给出正确答案
    """,
)
def card_guess_check_hard_route(
    cards: Annotated[
        str,
        StringConstraints(
            pattern=r"^[♠♥♣♦](10|[2-9JQKA])(,[♠♥♣♦](10|[2-9JQKA])){3}$",
            strip_whitespace=True,
        ),
        Query(
            description="4张牌,格式为花色+大小, 比如：cards=♥Q,♣A,♦J,♠6",
            example="♥Q,♣A,♦J,♠6",
        ),
    ],
    answer: Annotated[
        str,
        StringConstraints(
            pattern=r"^[♠♥♣♦](10|[2-9JQKA])$",
            strip_whitespace=True,
        ),
        Query(
            description="隐藏的牌,格式为花色+大小, 比如：answer=♥Q",
            example="♥Q",
        ),
    ],
):
    return card_guess_check_hard(cards, answer)
